package Pemilik;

import Kasir.Kasir;
import Transaksi.TransaksiForm;
import KategoriBarang.Kategori;
import KategoriBarang.KategoriKontrol;
import java.sql.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author  Astri
 */
public class TambahKasirForm extends javax.swing.JFrame {

    /** Creates new form KategoriForm */
    public TambahKasirForm() {
        try {
            initComponents();
            simpanButton.setEnabled(true);
            

            List<Kasir> kasir = TambahKasirKontrol.getKoneksiKontrol().getAllKasir();
            TambahKasirTabelModel model = new TambahKasirTabelModel(kasir);
            kasirTable.setModel(model);

        } catch (Exception e) {
            Logger.getLogger(TambahKasirForm.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        passwordLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        idKasirLabel = new javax.swing.JLabel();
        namaKasirLabel = new javax.swing.JLabel();
        namaKasirField = new javax.swing.JTextField();
        simpanButton = new javax.swing.JButton();
        batalButton = new javax.swing.JButton();
        keluarButton = new javax.swing.JButton();
        alamatLabel = new javax.swing.JLabel();
        alamatField = new javax.swing.JTextField();
        passwordLabel = new javax.swing.JLabel();
        statusLabel = new javax.swing.JLabel();
        statusField = new javax.swing.JTextField();
        id_kasir_field = new javax.swing.JTextField();
        passwordField = new javax.swing.JPasswordField();
        jScrollPane2 = new javax.swing.JScrollPane();
        kasirTable = new javax.swing.JTable();
        jLabel5 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        kembaliButton = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        passwordLabel1.setText("Password");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(204, 204, 255));

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));

        idKasirLabel.setText("Id_Kasir");

        namaKasirLabel.setText("Nama_Kasir");

        namaKasirField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                namaKasirFieldActionPerformed(evt);
            }
        });
        namaKasirField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                namaKasirFieldKeyTyped(evt);
            }
        });

        simpanButton.setText("Simpan");
        simpanButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                simpanButtonActionPerformed(evt);
            }
        });

        batalButton.setText("Batal");
        batalButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                batalButtonActionPerformed(evt);
            }
        });

        keluarButton.setText("Keluar");
        keluarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                keluarButtonActionPerformed(evt);
            }
        });

        alamatLabel.setText("Alamat");

        alamatField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                alamatFieldActionPerformed(evt);
            }
        });

        passwordLabel.setText("Password");

        statusLabel.setText("Status");

        id_kasir_field.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                id_kasir_fieldActionPerformed(evt);
            }
        });
        id_kasir_field.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                id_kasir_fieldKeyTyped(evt);
            }
        });

        passwordField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                passwordFieldActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(293, Short.MAX_VALUE)
                .addComponent(simpanButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(batalButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(keluarButton)
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(65, 65, 65)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(namaKasirLabel)
                    .addComponent(idKasirLabel)
                    .addComponent(passwordLabel)
                    .addComponent(alamatLabel)
                    .addComponent(statusLabel))
                .addGap(48, 48, 48)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(passwordField, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
                    .addComponent(id_kasir_field, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
                    .addComponent(statusField, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
                    .addComponent(alamatField, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
                    .addComponent(namaKasirField, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE))
                .addGap(81, 81, 81))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(idKasirLabel)
                    .addComponent(id_kasir_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(namaKasirLabel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(namaKasirField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(alamatField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(alamatLabel))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(9, 9, 9)
                        .addComponent(passwordLabel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(statusField, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(keluarButton)
                            .addComponent(batalButton)
                            .addComponent(simpanButton)))
                    .addComponent(statusLabel))
                .addContainerGap())
        );

        kasirTable.setAutoCreateRowSorter(true);
        kasirTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Id Kasir", "Nama Kasir", "Alamat", "Password", "Status"
            }
        ));
        kasirTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                kasirTableMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(kasirTable);

        jLabel5.setText("Data Kasir");

        jPanel2.setBackground(new java.awt.Color(204, 204, 255));

        jLabel3.setBackground(new java.awt.Color(204, 204, 255));
        jLabel3.setFont(new java.awt.Font("Tw Cen MT Condensed Extra Bold", 0, 24));
        jLabel3.setText("TAMBAH KASIR");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(168, 168, 168)
                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(193, 193, 193))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE)
                .addContainerGap())
        );

        kembaliButton.setText("Kembali");
        kembaliButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kembaliButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addContainerGap(443, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 482, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(388, Short.MAX_VALUE)
                .addComponent(kembaliButton)
                .addGap(45, 45, 45))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(kembaliButton)
                .addGap(95, 95, 95))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-513)/2, (screenSize.height-474)/2, 513, 474);
    }// </editor-fold>//GEN-END:initComponents

private void namaKasirFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_namaKasirFieldActionPerformed
// TODO add your handling code here:
      


}//GEN-LAST:event_namaKasirFieldActionPerformed

private void keluarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keluarButtonActionPerformed
// TODO add your handling code here:
    this.dispose();
}//GEN-LAST:event_keluarButtonActionPerformed

private void batalButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_batalButtonActionPerformed
// TODO add your handling code here:
    id_kasir_field.setText("");
    namaKasirField.setText("");
    alamatField.setText("");
    passwordField.setText("");
    statusField.setText("");
    
    id_kasir_field.setEditable(true);
    simpanButton.setText("Simpan");
}//GEN-LAST:event_batalButtonActionPerformed

private void simpanButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_simpanButtonActionPerformed

    try {

        String id = id_kasir_field.getText();
        String nama = namaKasirField.getText();
        String almt = alamatField.getText();
        String paswd = passwordField.getText();
        String stt = statusField.getText();
        
        Kasir kasirBaru = new Kasir();
        kasirBaru.setId_Kasir(id);
        kasirBaru.setNama_Kasir(nama);
        kasirBaru.setAlamat(almt);
        kasirBaru.setPassword(paswd);
        kasirBaru.setStatus(stt);

        TambahKasirKontrol kasirKontrol = TambahKasirKontrol.getKoneksiKontrol();
        if (simpanButton.getText() == "Simpan") {
        
            kasirKontrol.simpanKasirStatement(kasirBaru);
            List<Kasir> kasir = TambahKasirKontrol.getKoneksiKontrol().getAllKasir();
            TambahKasirTabelModel model = new TambahKasirTabelModel(kasir);
            kasirTable.setModel(model);
            JOptionPane.showMessageDialog(this, "Kasir baru atas nama " + nama + " berhasil disimpan");

        } 

    } catch (SQLException ex) {
        Logger.getLogger(TambahKasirForm.class.getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(this, "Data gagal disimpan");

    }
    batalButton.doClick();
    

}//GEN-LAST:event_simpanButtonActionPerformed

private void kasirTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_kasirTableMouseClicked
    int row = kasirTable.getSelectedRow();
    String idgroup = kasirTable.getValueAt(row, 0).toString();
    String namagroup = kasirTable.getValueAt(row, 1).toString();
    String almtgroup = kasirTable.getValueAt(row, 2).toString();
    String paswdgroup = kasirTable.getValueAt(row, 3).toString();
    String sttgroup = kasirTable.getValueAt(row, 4).toString();
   
    namaKasirField.setText(namagroup);
    id_kasir_field.setText(idgroup);
    alamatField.setText(almtgroup);
    passwordField.setText(paswdgroup);
    statusField.setText(sttgroup);

    id_kasir_field.setEditable(false);
    namaKasirField.requestFocus();

    simpanButton.setEnabled(true);
    simpanButton.setText("Update");
    //hapusButton.setEnabled(true);


}//GEN-LAST:event_kasirTableMouseClicked

private void idKasirFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_idKasirFieldActionPerformed
}//GEN-LAST:event_idKasirFieldActionPerformed

private void idKasirFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_idKasirFieldKeyReleased
    //tanpa dienter kode dimasukkan , nama kategori muncul
    Kasir kasir = new Kasir();
    String kode = id_kasir_field.getText();
    kasir.setId_Kasir(kode);

    try {
        kasir = TambahKasirKontrol.getKoneksiKontrol().cariKasir(kasir);
        String namaKasir = kasir.getNama_Kasir();
        namaKasirField.setText(namaKasir);
    } catch (Exception e) {
        Logger.getLogger(TambahKasirForm.class.getName()).log(Level.SEVERE, null, e);

    }
}//GEN-LAST:event_idKasirFieldKeyReleased

private void idKasirFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_idKasirFieldKeyPressed
    //harus pakai enter baru nama kategori muncul
}//GEN-LAST:event_idKasirFieldKeyPressed

private void idKasirFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_idKasirFieldKeyTyped
    //harus pakai enter baru nama kategori muncul
    if (evt.getSource().equals(id_kasir_field)) {
        if (id_kasir_field.getText().length() > 6) {
            evt.setKeyChar('\n');
            JOptionPane.showMessageDialog(null, "id maksimal 7 karakter");

        }
    }

}//GEN-LAST:event_idKasirFieldKeyTyped

private void namaKasirFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_namaKasirFieldKeyTyped
    if (!id_kasir_field.getText().isEmpty()) {
        simpanButton.setEnabled(true);
    } else {
        JOptionPane.showMessageDialog(null, "id harus dimasukkan");
    }

}//GEN-LAST:event_namaKasirFieldKeyTyped

private void kembaliButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kembaliButtonActionPerformed
    // TODO add your handling code here:
    new FrameUtamaPemilik().setVisible(true);
            this.setVisible(false);
}//GEN-LAST:event_kembaliButtonActionPerformed

private void alamatFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alamatFieldActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_alamatFieldActionPerformed

private void id_kasir_fieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_id_kasir_fieldActionPerformed

 Kasir kasir = new Kasir();
    String kode = id_kasir_field.getText();
    kasir.setId_Kasir(kode);

    try {
        kasir = TambahKasirKontrol.getKoneksiKontrol().cariKasir(kasir);
        String namaKasir = kasir.getNama_Kasir();
        namaKasirField.setText(namaKasir);
    } catch (Exception e) {
        Logger.getLogger(TambahKasirForm.class.getName()).log(Level.SEVERE, null, e);

    }
    // TODO add your handling code here:
}//GEN-LAST:event_id_kasir_fieldActionPerformed

private void id_kasir_fieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_id_kasir_fieldKeyTyped
   if (evt.getSource().equals(id_kasir_field)) {
        if (id_kasir_field.getText().length() > 6) {
            evt.setKeyChar('\n');
            JOptionPane.showMessageDialog(null, "id maksimal 7 karakter");

        }
    }    // TODO add your handling code here:
}//GEN-LAST:event_id_kasir_fieldKeyTyped

private void passwordFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_passwordFieldActionPerformed
 
    // TODO add your handling code here:s
}//GEN-LAST:event_passwordFieldActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
               new TambahKasirForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField alamatField;
    private javax.swing.JLabel alamatLabel;
    private javax.swing.JButton batalButton;
    private javax.swing.JLabel idKasirLabel;
    private javax.swing.JTextField id_kasir_field;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable kasirTable;
    private javax.swing.JButton keluarButton;
    private javax.swing.JButton kembaliButton;
    private javax.swing.JTextField namaKasirField;
    private javax.swing.JLabel namaKasirLabel;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JLabel passwordLabel1;
    private javax.swing.JButton simpanButton;
    private javax.swing.JTextField statusField;
    private javax.swing.JLabel statusLabel;
    // End of variables declaration//GEN-END:variables
}
